package wsi.gui.resolve.task;

import java.io.File;

import javafx.concurrent.Task;
import wsi.exp.ExpFolderController;
import wsi.gui.resolve.model.FunctionStatTuple;
import wsi.ws.WebServices;

public class UpdateInformationTask extends Task<String> {

	@Override
	protected String call() throws Exception {
		updateMessage("Updating...");

		File dir = ExpFolderController.shared().getFolderForExp5();

		int j, f, p;

		j = f = p = 0;

		for (WebServices ws : WebServices.values()) {
			if (isCancelled())
				return null;

			File ws_dir = new File(dir, ws.name());

			if (!ws_dir.exists() || !ws_dir.isDirectory())
				continue;

			FunctionStatTuple tuple;

			for (File d : ws_dir.listFiles()) {
				if (isCancelled())
					return null;

				if (!d.isDirectory())
					continue;

				tuple = new FunctionStatTuple(ws.name(), d);

				if (!tuple.isValid())
					continue;

				p += tuple.getNumberOfPairs();
				j += tuple.getNumberOfJudgedPairs();
				f += tuple.getNumberOfFiles();
			}
		}

		String res = String.format(
				"In total %d pairs in %d files, %d pairs judged.", p, f, j);
		updateMessage(res);

		return res;
	}

}
